package com.yanqu.road.server.logic.platform;


import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.enums.eReduceItemStatus;
import com.yanqu.road.entity.platform.PlatformReduceItemOrder;
import com.yanqu.road.logic.bussiness.platform.ReduceItemBussiness;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.ReduceItemModule;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class ReduceItemManager {

    private static Logger logger = LogManager.getLogger(ReduceItemManager.class.getName());

    /**
     * 扣除道具发往玩家消息队列
     */
    public static String msgNotifyReduceItem(String orderNo) {
        JSONObject resultObj = new JSONObject();
        PlatformReduceItemOrder reduceItemOrder = ReduceItemBussiness.getPlatformReduceItemOrder(orderNo);
        if (null == reduceItemOrder) {
            resultObj.put("state", 0);
            resultObj.put("msg", "ReduceItemOrder订单号:" + orderNo + "不存在");
            return resultObj.toJSONString();
        }

        long userId = reduceItemOrder.getUserId();
        GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);
        if (player == null) {
            // 玩家不在线加载出来
            player = GamePlayerMgr.getPlayer(userId);
        }
        if (player != null) {
            // 处理扣道具
            return player.getModule(ReduceItemModule.class).doReduceItem(reduceItemOrder);
        } else {
            resultObj.put("state", 0);
            resultObj.put("msg", "ReduceItemOrder订单玩家不在线" + orderNo);
            return resultObj.toJSONString();
        }
    }
}
